package com.bauer.base.jmm;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author ：rui.wei
 * @Date ：Created in 10:43 2019/4/26
 * @Description： 主要验证 线程保存的是本地内存的备份问题
 */
public class JmmDemo {

    /**
     * 不管i 是否被 volatile修饰   i都是以第一次读到的值为准
     */
    private static volatile int i = 1;

    private static boolean falg = false;

    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 10, 0L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(4));

    static class ReadDemo implements Runnable {

        @Override
        public void run() {
            int j = i;
            System.out.println("readj 1:" + j);
        }
    }

    static class WriteDemo implements Runnable {

        private int num;

        public WriteDemo(int num) {
            this.num = num;
        }

        @Override
        public void run() {
            System.out.println("write");
            i = 10;

        }
    }

    public static void main(String[] args) {
        threadPoolExecutor.execute(new ReadDemo());
        threadPoolExecutor.execute(new ReadDemo());
        threadPoolExecutor.execute(new ReadDemo());
        threadPoolExecutor.execute(new ReadDemo());
        threadPoolExecutor.execute(new WriteDemo(2));
        threadPoolExecutor.execute(new ReadDemo());
        threadPoolExecutor.shutdown();
    }

}
